
import UIKit
import Moya

typealias SuccessHandlerType = ((NSDictionary) -> Void)

enum FAMNetUploadApi {
    case uploadData(data:Data)
}

extension FAMNetUploadApi: TargetType {
    var FAM_cachePolicy: URLRequest.CachePolicy? {
        return .useProtocolCachePolicy
    }
    
    func FAM_getCurrentUrl() -> String {
        switch self {
        case .uploadData:
//            return "https://up.cameracalctr.com/foodmate/"
            return "http://tbup.preproduce.net/foodmate/"
        }
    }
    
    var baseURL: URL {
        return URL(string: FAM_getCurrentUrl())!
    }
    var path: String {
        switch self {
        case .uploadData:
            return "upload"
        }
    }
    
    var method: Moya.Method {
        return .post
    }
    
    var task: Moya.Task {
        switch self {
        case .uploadData(let data):
            let timestamp = Int(Date().timeIntervalSince1970)
            let fileName = (FAMMD5().MD5("\(timestamp)\(data.count)")) + ".png"
            let formData = MultipartFormData(provider: .data(data), name: "file", fileName: fileName, mimeType: "image/jpeg")
            return .uploadMultipart([formData])
        }
    }
    
    var headers: [String : String]? {
        return FAMNetHeaderConfig.shared.FAM_getCommonHeaders(otherParams: [:])
    }
}
